package com.qfedu.hotelssm.controller;

import com.alibaba.excel.EasyExcel;
import com.qfedu.hotelssm.dto.PassengersExportDto;
import com.qfedu.hotelssm.entity.Passengers;
import com.qfedu.hotelssm.service.intf.PassengersService;
import com.qfedu.hotelssm.vo.PageBean;
import com.qfedu.hotelssm.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author mk
 * @date 2022/8/28  21:12
 */
@RestController
@RequestMapping("/api/passengers/")
public class PassengersController {
    @Autowired
    private PassengersService service;
    //新增
    @PostMapping("save.do")
    public R save(Passengers passengers){
        return service.save(passengers);
    }
    //修改
    @PostMapping("update.do")
    public R update(Passengers passengers){
        return service.update(passengers);
    }
    //删除
    @GetMapping("del.do")
    public R del(int id){
        return service.delete(id);
    }
    //查询
    @GetMapping("page.do")
    public PageBean page(String name, int page, int limit){
        return service.selectPage(name, page, limit);
    }
    //批量删除
    @PostMapping("delids.do")
    public R dels(int[] ids){
        return service.delIds(ids);
    }
    //导出Excel
    @GetMapping("download.do")
    public void download(HttpServletResponse response) throws IOException {
        //1.设置 响应消息头 下载
        response.setHeader("content-disposition","attachment;filename=passengers-"+System.currentTimeMillis()+".xlsx");
        //2.查询学员数据
        List<PassengersExportDto> list=service.all();
        //3.实现数据写出 Excel生成
        EasyExcel.write(response.getOutputStream(),PassengersExportDto.class).sheet(System.currentTimeMillis()+"").doWrite(list);
    }
    //导入数据
    @PostMapping("upload.do")
    public R upload(MultipartFile file) throws IOException {
        return service.upload(file);
    }
}
